package com.ll.wechat.friends.adapter;

import java.util.HashMap;
import java.util.List;
import org.jivesoftware.smack.roster.RosterEntry;
import com.ll.wechat.R;
import com.ll.wechat.base.utils.thread.ThreadPoolUtil;
import com.ll.wechat.friends.utils.PinyinUtils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 通讯录列表适配器
 * 
 * @ClassName: FriendsListAdapter
 * @author yeliangliang
 * @date 2015-7-26 下午6:20:29
 */
public class FriendsListAdapter extends BaseAdapter {

	private Context mContext;
	private List<RosterEntry> mList;
	private LayoutInflater inflater;
	private HolderView holderView = null;
	public HashMap<String, Integer> positionMap;

	public FriendsListAdapter(Context context, List<RosterEntry> list) {
		this.mContext = context;
		this.mList = list;
		this.inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holderView = new HolderView();
			convertView = inflater.inflate(R.layout.item_friendslist, null);
			holderView.tv = (TextView) convertView.findViewById(R.id.tv_user_name);
			holderView.img = (ImageView) convertView.findViewById(R.id.img_user_head);
			holderView.tv_letter = (TextView) convertView.findViewById(R.id.tv_letter);
			convertView.setTag(holderView);
		} else {
			holderView = (HolderView) convertView.getTag();
		}
		String letter = "";
		// 获取首字母
		letter = PinyinUtils.converterToFirstSpell(mList.get(position).getName()).substring(0, 1);
		if (position == 0) {// 第一行必须是字母提示栏
			holderView.tv_letter.setVisibility(View.VISIBLE);
			holderView.tv_letter.setText(letter);
			positionMap.put(letter, position);
		} else {// 从第二行开始就要判断
			String lastLetter = "";
			// 获取首字母
			lastLetter = PinyinUtils.converterToFirstSpell(mList.get(position - 1).getName())
					.substring(0, 1);
			if (letter.equals(lastLetter)) {
				// 首字母相同
				holderView.tv_letter.setVisibility(View.GONE);
			} else {
				// 首字母不相同
				holderView.tv_letter.setVisibility(View.VISIBLE);
				holderView.tv_letter.setText(letter);
				positionMap.put(letter, position);
			}
		}
		holderView.tv.setText(mList.get(position).getName());
		return convertView;
	}

	private class HolderView {
		private TextView tv;
		private ImageView img;
		private TextView tv_letter;
	}

	// 刷新ListView
	public void myNotifyDataChange() {

		Runnable runnable = new Runnable() {

			@Override
			public void run() {
				positionMap = new HashMap<String, Integer>();
				for (int position = 0; position < mList.size(); position++) {

					String letter = "";
					// 获取首字母
					letter = PinyinUtils.converterToFirstSpell(mList.get(position).getName())
							.substring(0, 1);
					if (position == 0) {// 第一行必须是字母提示栏
						positionMap.put(letter, position);
					} else {// 从第二行开始就要判断
						String lastLetter = "";
						// 获取首字母
						lastLetter = PinyinUtils.converterToFirstSpell(
								mList.get(position - 1).getName()).substring(0, 1);
						if (!letter.equals(lastLetter)) {
							// 首字母不相同
							positionMap.put(letter, position);
						}
					}
				}
			}
		};
		ThreadPoolUtil.insertTaskToCatchPool(runnable);
		this.notifyDataSetChanged();
	}

	/**
	 * 返回坐标记录器
	 * 
	 * @return
	 * @author yeliangliang
	 * @date 2015-7-27 下午9:35:48
	 * @version V1.0
	 * @return HashMap<String,Integer>
	 */
	public HashMap<String, Integer> getPositionMap() {
		return positionMap;
	}

}
